package com.dycx.user.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.user.converter.CollectConverter;
import com.dycx.user.mapper.CollectMapper;
import com.dycx.user.dto.CollectDTO;
import com.dycx.user.entity.CollectDO;
import com.dycx.user.service.CollectService;
import com.dycx.user.vo.CollectVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Null;
import java.util.List;

/**
 * 收藏(Collect)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-12-29 11:37:20
 */

@Service
@AllArgsConstructor
public class CollectImpl extends ServiceImpl<CollectMapper, CollectDO> implements CollectService {

    private CollectMapper collectMapper;

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<CollectVO> getPage(CollectDTO dto) {
        LambdaQueryWrapper<CollectDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(dto.getUid()), CollectDO::getUid, dto.getUid());
        wrapper.orderByDesc(CollectDO::getCollectId);

        Page<CollectDO> entityPage = page(new Page<>(dto.getPageIndex(), dto.getPageSize()), wrapper);

        return CollectConverter.entityToVoPage(entityPage);
    }

    /**
     * 查询详情
     *
     * @param collectId 主键
     * @return 查询结果
     */
    @Override
    public CollectVO load(Integer collectId) {
        CollectDO entity = getById(collectId);

        return CollectConverter.entityToVo(entity);
    }


    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean create(CollectDTO dto) {
        CollectDO entity = BeanUtil.toBean(dto, CollectDO.class);
        return save(entity);
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean edit(CollectDTO dto) {
        CollectDO entity = BeanUtil.toBean(dto, CollectDO.class);
        return updateById(entity);
    }

    /**
     * 删除
     *
     * @param dto
     * @return
     */
    @Override
    public Boolean delete(CollectDTO dto) {
        LambdaQueryWrapper<CollectDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectDO::getUid, dto.getUid());
        wrapper.eq(CollectDO::getBizType, dto.getBizType());
        wrapper.eq(CollectDO::getBizId, dto.getBizId());

        return remove(wrapper);
    }

    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return collectMapper.getMaxSort();
    }

    @Override
    public Boolean getIsCollect(CollectDTO dto) {
        LambdaQueryWrapper<CollectDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CollectDO::getUid, dto.getUid());
        wrapper.eq(CollectDO::getBizType, dto.getBizType());
        wrapper.eq(CollectDO::getBizId, dto.getBizId());

        return count(wrapper) > 0;
    }
}

